package cn.alcatraz.product.service.impl;

import cn.alcatraz.basic.utils.BaiduAuditUtils;

import cn.alcatraz.product.domain.Product;
import cn.alcatraz.product.domain.ProductDetail;
import cn.alcatraz.product.domain.ProductOnlineAuditLog;
import cn.alcatraz.product.mapper.ProductDetailMapper;
import cn.alcatraz.product.mapper.ProductOnlineAuditLogMapper;
import cn.alcatraz.product.mapper.ProductMapper;
import cn.alcatraz.product.service.IProductService;
import cn.alcatraz.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author alcatraz
 * @since 2022-07-21
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    @Autowired
    private ProductDetailMapper productDetailMapper;


    @Override
    public void offsale(List<Long> ids) {
        productMapper.offsale(ids);
    }

    @Override
    public void onsale(List<Long> ids) {
        for (Long id : ids) {
            //得到每个宠物基本信息对象
            Product product = productMapper.loadById(id);
            StringBuffer sbf = new StringBuffer();
            //调用百度AI进行审核
            if (!BaiduAuditUtils.TextCensor(product.getName())){
                sbf.append("名称不合法，违规名称：" + product.getName());
            }
           /* if (!BaiduAuditUtils.ImgCensor(BaseConstant.FAST_DFS_IP + product.getResources().split(",")[0])){
                sbf.append("图片不合法，违规名称：" + product.getResources().split(",")[0]);
            }*/
            if (sbf.length() > 0){
                //违规，保存审核日志
                ProductOnlineAuditLog productOnlineAuditLog = new ProductOnlineAuditLog();
                productOnlineAuditLog.setProductId(id);
                productOnlineAuditLog.setNote(sbf.toString());
                productOnlineAuditLogMapper.save(productOnlineAuditLog);
                continue;
            }
            productMapper.onsale(id);
        }
    }

    @Override
    public void delete(Long id) {
        productMapper.delete(id);
        productDetailMapper.deleteByProductId(id);
    }

    @Override
    public void save(Product product) {
        productMapper.save(product);

        ProductDetail detail = product.getDetail();

        if(detail != null){

            detail.setProductId(product.getId());

            productDetailMapper.save(detail);
        }

    }

    @Override
    public void update(Product product) {
        productMapper.update(product);

        ProductDetail detail = product.getDetail();

        detail.setProductId(product.getId());

        productDetailMapper.update(detail);
    }

}
